package org.forum.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.struts2.json.annotations.JSON;

@Entity
@Table(name = "forum")
public class Forum implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7460547514107317804L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(nullable = false, unique = true)
	private Integer forumId;

	@Column(nullable = false, unique = true)
	private String title;

	private Integer numberOfFollows;

	// 需要对应时间戳类型
	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable = false)
	private Date openTime;

	private String introduction;

	// 分类
	private String classification;

	@ManyToMany(targetEntity = User.class)
	@JoinTable(name = "user_forum", joinColumns = @JoinColumn(name = "forumId", referencedColumnName = "forumId"), inverseJoinColumns = @JoinColumn(name = "userId", referencedColumnName = "userId"))
	private Set<User> users = new HashSet<>();

	@OneToMany(targetEntity = MainTopic.class, mappedBy = "forum")
	private Set<MainTopic> mainTopics = new HashSet<>();

	@OneToOne(targetEntity = Icon.class)
	// add foreign key
	@JoinColumn(name = "iconId", referencedColumnName = "iconId", unique = true)
	private Icon icon;

	@Column(nullable = false)
	private Integer administratorId;

	public Forum() {
	}

	public Forum(String title, Date openTime, String introduction, int administratorId) {
		this.title = title;
		this.openTime = openTime;
		this.introduction = introduction;
		this.administratorId = administratorId;
	}

	public Integer getForumId() {
		return forumId;
	}

	public void setForumId(Integer forumId) {
		this.forumId = forumId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Integer getNumberOfFollows() {
		return numberOfFollows;
	}

	public void setNumberOfFollows(Integer numberOfFollows) {
		this.numberOfFollows = numberOfFollows;
	}

	public Date getOpenTime() {
		return openTime;
	}

	public void setOpenTime(Date openTime) {
		this.openTime = openTime;
	}

	public String getIntroduction() {
		return introduction;
	}

	public void setIntroduction(String introduction) {
		this.introduction = introduction;
	}

	public Integer getAdministratorId() {
		return administratorId;
	}

	public void setAdministratorId(Integer administratorId) {
		this.administratorId = administratorId;
	}

	public String getClassification() {
		return classification;
	}

	public void setClassification(String classification) {
		this.classification = classification;
	}
	@JSON(serialize=false)
	public Set<User> getUsers() {
		return users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}
	@JSON(serialize=false)
	public Set<MainTopic> getMainTopics() {
		return mainTopics;
	}

	public void setMainTopics(Set<MainTopic> mainTopics) {
		this.mainTopics = mainTopics;
	}

	public Icon getIcon() {
		return icon;
	}

	public void setIcon(Icon icon) {
		this.icon = icon;
	}

}
